extends StaticBody2D

@onready var damage_receiver = $DamageReceiver
@export var knockback_intensity : float
@onready var sprite = $Sprite2D
var velocity := Vector2.ZERO

enum Status{IDLE,DIE}

const GRAVITY := 600.00
var height := 0.00
var height_speed := 0.00
var status = Status.IDLE

func _ready() -> void:
	damage_receiver.damage_receiver.connect(on_receive_damage.bind())

func _process(delta: float) -> void:
	position += velocity * delta
	sprite.position = Vector2.UP * height
	handle_air_time(delta)

func on_receive_damage(damage:int,direction:Vector2):
	if status == Status.IDLE:
		sprite.frame = 1
		status = Status.DIE
		height_speed = knockback_intensity * 3
		velocity += direction * knockback_intensity

func handle_air_time(delta : float):
	if status == Status.DIE:
		modulate.a -= delta
		height += height_speed * delta
		if height < 0:
			height = 0
			queue_free()
		else:
			height_speed -= GRAVITY * delta	
